﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.Health;
using Microsoft.Health.Events;
using Microsoft.Health.ItemTypes;
using Microsoft.Health.Web;

public partial class Subscriptions : HealthServicePage
{
    SubscriptionManager _manager;

    public Subscriptions()
    {
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        _manager = new SubscriptionManager(ApplicationConnection);
    }

    protected void Page_Prerender(object sender, EventArgs e)
    {
        _SubscriptionTable.DataSource = _manager.GetSubscriptions();
        _SubscriptionTable.DataBind();
    }

    protected void linkDeleteClick(Object sender, CommandEventArgs e)
    {
        Guid id = new Guid((string)e.CommandArgument);

        _manager.Unsubscribe(id);
    }

    protected void addClick(Object sender, EventArgs e)
    {
        NotificationAuthenticationSharedKeyInfo sharedKeyInfo = new NotificationAuthenticationSharedKeyInfo("TestVersion-1");
        sharedKeyInfo.Key = Convert.FromBase64String(ConfigurationManager.AppSettings["HVEventingSharedKeyValue"]);
            
        CommonSubscriptionData common = new CommonSubscriptionData(
            new HttpNotificationChannel(new Uri("https://localhost/notificationhandlerPage.ashx")), sharedKeyInfo);

        HealthRecordItemChangedSubscription subscription = new HealthRecordItemChangedSubscription(common);
        HealthRecordItemChangedFilter filter = new HealthRecordItemChangedFilter();
        filter.TypeIds.Add(BloodPressure.TypeId);
        subscription.Filters.Add(filter);
        _manager.Subscribe(subscription);
    }
}